package com.lyl.xxx.modules.web.account.controller;

import cn.hutool.core.bean.BeanUtil;
import com.lyl.xxx.common.annotation.Log;
import com.lyl.xxx.common.enums.BusinessType;
import com.lyl.xxx.modules.system.account.service.SysMenuService;
import com.lyl.xxx.modules.web.account.model.dto.MenuDTO;
import com.lyl.xxx.modules.web.account.model.dto.MenuTreeDTO;
import com.lyl.xxx.modules.web.account.model.vo.MenuTreeVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Mr.Luo
 * @date 2022-09-23 14:57
 * @desc 菜单管理
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/menu")
public class MenuController {
    private final SysMenuService sysMenuService;

    @GetMapping("/tree")
    public List<MenuTreeVO> menuTree(MenuTreeDTO dto) {
        return sysMenuService.menuTree(dto);
    }

    @DeleteMapping("/del/{menuId}")
    @Log(title = "菜单模块",businessType = BusinessType.DELETE)
    public boolean delById(@PathVariable Long menuId) {
        return sysMenuService.removeById(menuId);
    }

    /**
     * 更新菜单
     *
     * @param dto 请求参数
     * @return true/false
     */
    @PostMapping("/update")
    @Log(title = "菜单模块",businessType = BusinessType.UPDATE)
    public boolean updateMenu(@RequestBody @Valid MenuDTO dto) {
        return sysMenuService.updateMenu(dto);
    }


    @GetMapping("/get/{menuId}")
    public MenuTreeVO getById(@PathVariable Long menuId) {
        return BeanUtil.copyProperties(sysMenuService.getById(menuId), MenuTreeVO.class);
    }

    /**
     * 获取路由
     * @return 路由表
     */
    @GetMapping("/get/routers")
    public List<MenuTreeVO> getRouters(){
        return sysMenuService.getRouters();
    }
}
